import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:lmmi/common/services/user_service.dart';
import 'package:lmmi/common/entity/user.dart';

void main() {
  group('Username Length Limit Tests', () {
    setUpAll(() async {
      // 初始化 Hive 测试环境
      Hive.init('./test/hive_test_db');

      // 注册适配器
      if (!Hive.isAdapterRegistered(0)) {
        Hive.registerAdapter(UserEntityAdapter());
      }

      // 打开用户数据盒子
      await Hive.openBox<UserEntity>('users');
      await Hive.openBox('current_user');
    });

    tearDownAll(() async {
      // 清理测试数据
      await Hive.deleteFromDisk();
    });

    setUp(() async {
      // 每个测试前清空数据
      final userBox = Hive.box<UserEntity>('users');
      final currentUserBox = Hive.box('current_user');
      await userBox.clear();
      await currentUserBox.clear();
    });

    test(
      'should reject username longer than 6 characters during registration',
      () async {
        // 测试超过6个字符的用户名
        final longUsername = 'verylongusername'; // 16个字符
        final result = await UserService.register(
          username: longUsername,
          password: 'password123',
        );

        expect(result, isFalse);
        expect(UserService.isUsernameExists(longUsername), isFalse);
      },
    );

    test(
      'should accept username with exactly 6 characters during registration',
      () async {
        // 测试正好6个字符的用户名
        final username = 'user12'; // 6个字符
        final result = await UserService.register(
          username: username,
          password: 'password123',
        );

        expect(result, isTrue);
        expect(UserService.isUsernameExists(username), isTrue);
      },
    );

    test(
      'should accept username with less than 6 characters during registration',
      () async {
        // 测试少于6个字符的用户名
        final username = 'user1'; // 5个字符
        final result = await UserService.register(
          username: username,
          password: 'password123',
        );

        expect(result, isTrue);
        expect(UserService.isUsernameExists(username), isTrue);
      },
    );

    test(
      'should accept single character username during registration',
      () async {
        // 测试单个字符的用户名
        final username = 'a'; // 1个字符
        final result = await UserService.register(
          username: username,
          password: 'password123',
        );

        expect(result, isTrue);
        expect(UserService.isUsernameExists(username), isTrue);
      },
    );

    test('should handle empty username during registration', () async {
      // 测试空用户名
      final username = ''; // 0个字符
      final result = await UserService.register(
        username: username,
        password: 'password123',
      );

      expect(result, isTrue); // 空用户名在UserService层面是允许的，但在UI层面会被阻止
    });

    test('should handle Chinese characters in username', () async {
      // 测试中文字符用户名（6个中文字符）
      final username = '用户名测试用'; // 6个中文字符
      final result = await UserService.register(
        username: username,
        password: 'password123',
      );

      expect(result, isTrue);
      expect(UserService.isUsernameExists(username), isTrue);
    });

    test('should reject Chinese username longer than 6 characters', () async {
      // 测试超过6个中文字符的用户名
      final username = '用户名测试用户名'; // 9个中文字符
      final result = await UserService.register(
        username: username,
        password: 'password123',
      );

      expect(result, isFalse);
      expect(UserService.isUsernameExists(username), isFalse);
    });

    test('should handle mixed characters in username', () async {
      // 测试混合字符用户名（英文+数字+符号，6个字符）
      final username = 'u1@#\$%'; // 6个混合字符
      final result = await UserService.register(
        username: username,
        password: 'password123',
      );

      expect(result, isTrue);
      expect(UserService.isUsernameExists(username), isTrue);
    });

    test(
      'should reject mixed characters username longer than 6 characters',
      () async {
        // 测试超过6个混合字符的用户名
        final username = 'user123@#\$'; // 10个混合字符
        final result = await UserService.register(
          username: username,
          password: 'password123',
        );

        expect(result, isFalse);
        expect(UserService.isUsernameExists(username), isFalse);
      },
    );

    test('should handle emoji characters in username', () async {
      // 测试包含emoji的用户名（emoji字符在Dart中可能占用多个字符位置）
      final username = '😀😁😂'; // 3个emoji字符，但在Dart中可能被计算为6个字符
      print('Emoji username length: ${username.length}'); // 调试输出
      final result = await UserService.register(
        username: username,
        password: 'password123',
      );

      // 根据实际长度判断结果
      if (username.length <= 6) {
        expect(result, isTrue);
        expect(UserService.isUsernameExists(username), isTrue);
      } else {
        expect(result, isFalse);
        expect(UserService.isUsernameExists(username), isFalse);
      }
    });
  });
}
